package org.example;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.codehaus.jettison.json.JSONObject;
import org.example.model.student.Gender;
import org.example.model.student.Student;
import org.example.model.weather.JsonRootBean;
import org.example.ws.HelloService;
import org.junit.Test;

import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;
import java.io.*;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;


public class Client {
	@Test
	public void testGet() {
		Student student = WebClient
				.create("http://localhost:8080/Student/GetByID/1191024012")
				.accept(MediaType.APPLICATION_JSON).
				get(Student.class);
		System.out.println(student);
	}
	
	@Test
	public void testGetAll(){
		List<Student> students = WebClient
				.create("http://localhost:8080/Student/GetAll")
				.accept(MediaType.APPLICATION_JSON)
				.get(new GenericType<List<Student>>(){});
		for (Student student:students){
			System.out.println(student);
		}
	}
	@Test
	public void testSave() {
		Student student = new Student("1","2","3","4", Gender.F);
		WebClient
				.create("http://localhost:8080/Student")
				.type(MediaType.APPLICATION_JSON)
				.post(student);
	}
	
	@Test
	public void testDelete(){
		WebClient.create("http://localhost:8080/Student/GetByID/1").delete();
	}
	
	@Test
	public void testReadHelp() throws IOException {
		File file = new File("src/main/resources/help_EN.txt");
		FileReader fr;
		fr = new FileReader(file);
		
		char[] buffer = new char[1024];
		int count;
		while ((count=fr.read(buffer))!=-1){
			System.out.println(count);
			System.out.println(new String(buffer,0,count));
		}
		if (fr!=null)
			fr.close();
	}
	
	@Test
	public void name() {
		JSONObject jsonObject=new JSONObject();
	}
	
	@Test
	public void rand() {
		Random random = new Random();
		for (int i=1;i<=100;++i){
			System.out.println(random.nextDouble());
		}
	}
	@Test
	public void test2(){
		JaxWsProxyFactoryBean factory=new JaxWsProxyFactoryBean();
		//设置原创访问的地址
		factory.setAddress("http://localhost:8000/ws/hello");
		//设置接口类型
		factory.setServiceClass(HelloService.class);
		//对 接口 生产 代理 对象
		HelloService helloService = factory.create(HelloService.class);
//		System.out.println(helloService.getClass());
		for (int i=1;i<=100;++i){
			String content = helloService.hello("1");
			System.out.println(content);
		}
	}
	
	@Test
	public void gsonTest() throws FileNotFoundException {
		Gson gson = new GsonBuilder()
				.setDateFormat("yyyy-MM-dd HH:mm")
				.create();
		File file = new File("src/main/resources/weather.json");
		FileReader fileReader=new FileReader(file);
		JsonRootBean jsonRootBean = gson.fromJson(fileReader, JsonRootBean.class);
//		System.out.println(jsonRootBean);
		Date date = jsonRootBean.getLocation().getLocaltime();
		System.out.printf("更新时间：%s\n",new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss").format(date));
		System.out.printf("气温：%s ℃\n",jsonRootBean.getCurrent().getTemp_c());
		System.out.printf("风速: %s km/h\n",jsonRootBean.getCurrent().getWind_kph());
		System.out.printf("风向: ",jsonRootBean.getCurrent().getWind_dir());
		System.out.printf("降雨量: %s mm\n",jsonRootBean.getCurrent().getPrecip_mm());
		System.out.printf("湿度 %s\n",jsonRootBean.getCurrent().getHumidity());
		System.out.printf("云量: \n",jsonRootBean.getCurrent().getCloud());
		System.out.printf("能见度: %skm\n",jsonRootBean.getCurrent().getVis_km());
	}
	@Test
	public void jarFileTest() throws IOException {
		String name = "D:\\IdeaProjects\\WebService_Project\\out\\artifacts\\Client_jar\\Client.jar";
		JarFile jarFile=new JarFile(name);
		System.out.println(jarFile.getName());
		
		JarEntry jarEntry = jarFile.getJarEntry("help_EN.txt");
		System.out.println(jarEntry.getName());
		
		JarInputStream jarInputStream=new JarInputStream(jarFile.getInputStream(jarEntry));
		byte[] buff = new byte[1024];
		int count=-1;
		while ((count=jarInputStream.read(buff))!=-1){
			System.out.println("Aaaaaaa");
			System.out.println(new String(buff,0,count));
		}
	}
	
	@Test
	public void b() throws IOException {
		String path = "D:\\IdeaProjects\\WebService_Project\\out\\artifacts\\Client_jar\\Client.jar";
		JarFile zf = new JarFile(path);
		InputStream in = new BufferedInputStream(new FileInputStream(path));
		Charset gbk = Charset.forName("gbk");
		ZipInputStream zin = new ZipInputStream(in,gbk);
		ZipEntry ze;
		while((ze = zin.getNextEntry()) != null){
			if(ze.toString().endsWith("txt")){
				BufferedReader br = new BufferedReader(
						new InputStreamReader(zf.getInputStream(ze)));
				String line;
				while((line = br.readLine()) != null){
					System.out.println(line.toString());
				}
				br.close();
			}
			System.out.println();
		}
		zin.closeEntry();
	}
}
